<template>
  <div>
    <el-container>
      <el-header class="homeHeader">
        <div class="title">云E办系统</div>
        <!-- command事件 -->
        <el-dropdown class="userInfo" @command="commandHandler">
          <span class="el-dropdown-link">
            {{ user.name }}<i><img :src="user.userFace" /></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="userinfo">个人中心</el-dropdown-item>
            <el-dropdown-item command="setting">设置</el-dropdown-item>
            <el-dropdown-item command="logout">注销登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <!-- 菜单激活回调 跳转到main显示内容的事件 点击后找对应的index @select="menClick"-->
          <!-- <el-menu @select="menuClick"> -->

          <!-- router相当于启用了view-router路由模式，启用该模式之后，激活导航菜单的时候，会以index为path路径,进行路由的跳转 -->
          <el-menu router unique-opened>
            <!-- 做循环 ，循环整个路由，加入判断-->
            <!-- v-for v-if不能同时解决 加入template -->
            <!-- <template v-for="(item, index) in this.$router.options.routes"> -->
            <!-- 使用vuex获取routers -->
            <template v-for="(item, index) in routes">
              <el-submenu index="index +''" :key="index" v-if="!item.hidden">
                <template slot="title">
                  <i
                    style="color: #1accff; margin-right: 5px"
                    :class="item.iconCls"
                  ></i>
                  <span>{{ item.name }}</span>
                </template>
                <!-- index改为路由的跳转 -->
                <!-- 不写死 但是进行绑定 index.js的children  -->
                <!-- 再次做二次循环 -->
                <el-menu-item
                  :index="children.path"
                  v-for="(children, indexj) in item.children"
                  :key="indexj"
                >
                  {{ children.name }}
                </el-menu-item>

                <!-- <el-menu-item index="/test2">选项2</el-menu-item> -->
              </el-submenu>

              <!-- v-for v-if不能同时解决 加入template -->
            </template>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- v-ifh回到首页就不需要面包屑效果了 -->
          <el-breadcrumb separator-class="el-icon-arrow-right" v-if="this.$router.currentRoute.path!='/home'"> 
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item >{{this.$router.currentRoute.name}}</el-breadcrumb-item>
          </el-breadcrumb>
          <!-- 是首页才显示 -->
          <div class="homeWelcome" v-if="this.$router.currentRoute.path=='/home'">
            欢迎来到云e办系统
          </div>

          <!-- 准备一个路由的view -->
          <router-view class="homeRouterView" />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      user: JSON.parse(window.sessionStorage.getItem("user")), // 拿到user对象
    };
  },
  // methods: {
  //   menuClick(index) { // index事件点击的哪个index就是哪个index
  //     this.$router.push(index);
  //   }
  // }

  // 通过计算属性 从vuex获取routes
  computed: {
    routes() {
      return this.$store.state.routes;
    },
  },

  methods: {
    commandHandler(command) {
      if (command == "logout") {
        this.$confirm("此操作将注销登录,是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            //注销   接口
            this.postRequest("/logout");
            // 清除用户信息
            window.sessionStorage.removeItem("user");
            window.sessionStorage.removeItem("tokenStr");
            // 清空菜单   比如高级资料菜单这些存在vuex里面 登录的时候默认加载已有的数据
            this.$store.commit("initRoutes", []);
            // 跳转登录页
            this.$router.replace("/");
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消操作",
            });
          });
      }
    },
  },
};
</script>

<style scoped>
.homeHeader {
  background-color: #409eff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  box-sizing: border-box;
}
.homeHeader .title {
  font-size: 30px;
  font-family: 华文楷体;
  color: #ffffff;
}
.homeHeader .userInfo {
  cursor: pointer;
}
.homeHeader .userInfo img {
  width: 48px;
  height: 48px;
  border-radius: 24px;
  margin-left: 8px;
}
.homeWelcome {
  text-align: center;
  font-size: 30px;
  font-family: "华文楷体";
  color: #409eff;
  padding-top: 50px;
}
.homeRouterView {
  margin-top: 20px;
}
.el-dropdown-link {
  display: flex;
  align-items: center;
}
.el-dropdown-link img {
  width: 48px;
  height: 48px;
  border-radius: 24px;
  margin-left: 10px;
}
.el-icon-my-message {
  background: url("../assets/message.png") no-repeat;
  font-size: 16px;
  background-size: cover;
}
.el-icon-my-message:before {
  content: "替";
  font-size: 16px;
  visibility: hidden;
}
</style>